package de.op.projectc.objects;

import java.util.ArrayList;
import java.util.List;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Mesh;
import com.badlogic.gdx.graphics.VertexAttribute;
import com.badlogic.gdx.graphics.VertexAttributes.Usage;
import com.badlogic.gdx.math.Vector2;
import de.op.projectc.helper.CameraHelper;

public class Terrain {
	private final float MAPPING = 2;
	
	private ArrayList<Vector2> vertices;
	private ArrayList<Vector2> verticesUVMapping;
	private Mesh terrainMesh;
	
	public Mesh getTerrainMesh() {
		return terrainMesh;
	}

	public void setTerrainMesh(Mesh terrainMesh) {
		this.terrainMesh = terrainMesh;
	}

	Terrain(ArrayList<Vector2> vertices) {
		this.vertices = vertices;
		float[] allVertices = createUVMapping(this.vertices);
		this.terrainMesh = new Mesh(true, allVertices.length, 0, 
				new VertexAttribute(Usage.Position, 2, "a_position"),
				new VertexAttribute(Usage.TextureCoordinates, 2, "a_texCoords"));

        this.terrainMesh.setVertices(allVertices);
	}

	Terrain(Mesh mesh) {
		this.terrainMesh = mesh;
	}
	
	private float[] createUVMapping(ArrayList<Vector2> vertices) {
		this.verticesUVMapping = new ArrayList<Vector2>();
		float[] allVertices = new float[vertices.size() * 4];
        int i = 0;
        for(Vector2 vec : vertices) {
        	allVertices[i] = vec.x;
        	allVertices[i+1] = vec.y;
        	
        	float u = (vec.x / CameraHelper.getInstance().viewportWidth) * MAPPING;
        	float v = (vec.y / CameraHelper.getInstance().viewportHeight) * MAPPING;
        	allVertices[i+2] = u;//u Mapping
        	allVertices[i+3] = v;//v Mapping
   		 
        	this.verticesUVMapping.add(new Vector2(u, v));
            i += 4;
        }
		return allVertices;
	}

	//TODO wenn nicht mehr im Bildschirm (unter 0, dann l�schen)
	public void move(float valueX, float valueY) {
		float[] allVertices = new float[vertices.size() * 4];
        int j = 0;
        for(int i = 0; i < this.vertices.size(); i++) {
        	this.vertices.get(i).x += valueX;
        	this.vertices.get(i).y += valueY;
        	
        	/*
        	if(this.vertices.get(i).y < -10) {
        		this.vertices.remove(i);
        		this.verticesUVMapping.remove(i);
        		
        		continue;
        	}
        	*/
        	allVertices[j] = this.vertices.get(i).x;
        	allVertices[j+1] = this.vertices.get(i).y;
        	allVertices[j+2] = this.verticesUVMapping.get(i).x;//u Mapping
        	allVertices[j+3] = this.verticesUVMapping.get(i).y; //v Mapping

            j += 4;
        }
        
        this.terrainMesh.setVertices(allVertices);
	}
	
	public Vector2 getLastVertex() {
		return this.vertices.get(this.vertices.size() - 1);
	}
	
	public List<Vector2> getLastVertices(int howMany) {
		ArrayList<Vector2> vertices = new ArrayList<Vector2>();
		for(Vector2 vec : this.vertices.subList(this.vertices.size() - 1 - howMany, this.vertices.size() - 1))
			vertices.add(vec.cpy());
		
		return vertices;
	}
	
	public void dispose() {
		this.terrainMesh.dispose();
	}
}
